﻿// See https://aka.ms/new-console-template for more information

using System;
using System.Numerics;
using System.Collections;
using System.Collections.Generic;
using Microsoft.VisualBasic.FileIO;

int[]res = new int[20];
int cur = 0;
Console.WriteLine("请输入一个数来求他的质数因子");
int num=int.Parse(Console.ReadLine());

for (int i = 2; i <= num;)
{
    if (num % i == 0)
    {
        res[cur] = i;
        cur++;
        num /= i;
    }
    else i++;
}

for (int i = 0; i <cur; i++)
    Console.WriteLine(res[i]);


Console.WriteLine("请输入一组整数，用空格分隔：");

// 读取整行输入
string input = Console.ReadLine();

// 使用空格分割输入的字符串，并将其转换为整数数组
try
{
    int[] nums = input.Split(' ', StringSplitOptions.RemoveEmptyEntries)
                      .Select(int.Parse)
                      .ToArray();

    
    int max = nums.Max();
    string Max = max.ToString();
    int min = nums.Min();
    string Min = min.ToString();
    double ave = nums.Average();
    string Ave=ave.ToString();
    int sum = nums.Sum();
    string Sum=sum.ToString();

    Console.WriteLine("Max Min Ave Sum");
    Console.WriteLine(Max+" "+ Min+" "+ Ave+" "+ Sum);
}
catch (FormatException)
{
    Console.WriteLine("输入格式错误，请确保输入的是整数且用空格分隔。");
}



Console.WriteLine("找出2-100的素数");
ArrayList ans = new ArrayList();

int curmax = 100;
bool[] isPrime = new bool[curmax + 1];

// 初始化所有数为素数
for (int i = 2; i <= curmax; i++)
{
    isPrime[i] = true;
}

for (int i = 2; i <= Math.Sqrt(curmax); i++)
{
    if (isPrime[i])
    {
        // 将i标记为素数
        ans.Add(i);
        // 将i的所有倍数标记为非素数
        for (int j = i * i; j <= curmax; j += i)
        {
            isPrime[j] = false;
        }
    }
}

// 添加剩余的素数
for (int i = (int)Math.Sqrt(curmax) + 1; i <= curmax; i++)
{
    if (isPrime[i])
    {
        ans.Add(i);
    }
}

Console.WriteLine("素数为：");
foreach (int t in ans)
{
    Console.WriteLine(t);
}


bool IsTarget(ref int[][] a)
{
    int m = a.Length;
    if (m == 0) return true; // 空数组视为符合条件
    int n = a[0].Length;

    // 检查从左上到右下的对角线
    for (int t = 1 - m; t <= n - 1; t++)
    {
        List<int> diagonal = new List<int>();
        for (int i = 0; i < m; i++)
        {
            int j = i + t;
            if (j >= 0 && j < n)
            {
                diagonal.Add(a[i][j]);
            }
        }

        // 检查对角线元素是否都相等
        if (diagonal.Count > 0)
        {
            int first = diagonal[0];
            foreach (int value in diagonal)
            {
                if (value != first)
                {
                    return false;
                }
            }
        }
    }
    return true;
}


void PrintArray(int[][] arr)
{
    Console.WriteLine("\n输入数组结构：");
    for (int i = 0; i < arr.Length; i++)
    {
        Console.Write("Row {0}: [ ", i + 1);
        for (int j = 0; j < arr[i].Length; j++)
        {
            Console.Write(arr[i][j] + (j < arr[i].Length - 1 ? ", " : " "));
        }
        Console.WriteLine("]");
    }
}


Console.Write("输入行数: ");
int rows = int.Parse(Console.ReadLine());
int[][] a = new int[rows][];
int cols = -1;

for (int i = 0; i < rows; i++)
{
    while (true)
    {
        Console.Write($"第{i + 1}行元素（用空格分隔）: ");
        string input1 = Console.ReadLine().Trim();
        if (string.IsNullOrEmpty(input))
        {
            Console.WriteLine("错误：输入不能为空！");
            continue;
        }

        string[] parts = input1.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        a[i] = Array.ConvertAll(parts, int.Parse);

        // 列数校验
        if (cols == -1) cols = a[i].Length;
        if (a[i].Length != cols)
        {
            Console.WriteLine($"错误：每列必须包含{cols}个元素！");
            a[i] = null; // 清空错误输入
            continue;
        }
        break;
    }

}
PrintArray(a);
Console.WriteLine(IsTarget(ref a));